package com.cater.finance.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.cater.annotation.SystemControllerLog;
import com.cater.common.PageResult;
import com.cater.finance.entity.FsInvoice;
import com.cater.finance.service.FsInvoiceServiceImpl;
import com.cater.query.CommonQuery;
import com.cater.sys.entity.Dictionarydata;
import com.cater.sys.service.impl.DictionarydataServiceImpl;

/**
 * 描述：收藏控制层
 * 
 * @author yong
 * @date 2018/11/26
 */
@Controller
@RequestMapping("/fsInvoice")
public class FsInvoiceController {

	@Autowired
	private FsInvoiceServiceImpl service;
	@Autowired
	private DictionarydataServiceImpl dictService;

	/**
	 * @收藏管理列表视图
	 * @return
	 */
	@RequestMapping("/list")
	public String list() {
		return "finance/fsInvoice/list";
	}

	/**
	 * @收藏管理数据分页
	 * @param query
	 * @param request
	 * @return
	 */
	@RequestMapping("listData")
	@ResponseBody
	@SystemControllerLog(description = "查询收藏分页数据")
	public PageResult<FsInvoice> listData(CommonQuery query, HttpServletRequest request) {
		PageResult<FsInvoice> result = service.findPageResult(query);
		if (result.getRows() != null) {
			Dictionarydata dict = null;
			for (FsInvoice fInvoice : result.getRows()) {
				dict = new Dictionarydata();
				dict = dictService.getByTypeValue(fInvoice.getStatus(), "invoice_status");
				String status = dict.getValueName();
				if (StringUtils.isNotEmpty(status)) {
					fInvoice.setDictStatus(status);
				} else {
					fInvoice.setDictStatus("暂无");
				}
			}
		}
		return result;
	}

	/**
	 * 添加视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "addView")
	@RequiresPermissions("fsInvoice:add")
	public String addView() {
		return "finance/fsInvoice/add";
	}

	/**
	 * 添加操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "add")
	@RequiresPermissions("fsInvoice:add")
	@ResponseBody
	@SystemControllerLog(description = "添加收藏")
	public JSONObject add(FsInvoice t, CommonQuery query) {
		JSONObject json = new JSONObject();
		String id = service.saves(t);
		if (StringUtils.isNotBlank(id)) {
			json.put("code", 200);
			json.put("message", "success");
		} else {
			json.put("code", 999);
			json.put("message", "fail");
		}
		return json;
	}

	/**
	 * 修改视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "editView")
	@RequiresPermissions("fsInvoice:edit")
	public String editView(String id, Model model, CommonQuery query) {
		FsInvoice t = service.getById(id);
		model.addAttribute("fsInvoice", t);
		return "finance/fsInvoice/update";
	}

	/**
	 * 修改操作
	 * 
	 * @param t
	 * @return
	 */
	@RequestMapping(value = "update")
	@RequiresPermissions("fsInvoice:edit")
	@ResponseBody
	@SystemControllerLog(description = "修改收藏")
	public JSONObject update(FsInvoice t, CommonQuery query) {
		JSONObject json = new JSONObject();
		try {
			service.update(t);
			json.put("code", 200);
			json.put("message", "修改成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "修改失败");
		}
		return json;
	}

	/**
	 * 删除操作
	 * 
	 * @param id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	@RequiresPermissions("fsInvoice:delete")
	@SystemControllerLog(description = "删除收藏")
	public JSONObject delete(String id) {
		JSONObject json = new JSONObject();
		try {
			service.delete(id);
			json.put("code", 200);
			json.put("message", "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			json.put("code", 999);
			json.put("message", "删除失败");
		}
		return json;
	}

	/**
	 * 查看视图
	 * 
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "detailView")
	@RequiresPermissions("fsInvoice:detail")
	public String detailView(String id, Model model, CommonQuery query) {
		FsInvoice t = service.getById(id);
		model.addAttribute("fsInvoice", t);
		return "finance/fsInvoice/detail";
	}

}